<template>
  <div class="backGround">
    <div class="recommend-container">
      <van-nav-bar
        left-text="返回"
        @click-left="$router.back()"
        fixed
        title="我的推荐人"
      />
      <div class="title-padding"></div>
      <h2 class="van-doc-demo-block__title">添加联系人</h2>
      <van-contact-card type="add" @click="onAdd" />
      <h2 class="van-doc-demo-block__title">我的联系人</h2>
      <van-contact-card
        v-for="(item, index) in recommendList"
        :key="index"
        type="edit"
        :name="item.name"
        :tel="item.phoneNum"
        @click="onEdit(item)"
        class="contact-card"
      />
      <div class="bottomBox"></div>
    </div>
  </div>
</template>

<script>
/* eslint-disable */
import { Toast } from "vant";
import { buildURLWithParams, getLocal } from "@/utils";
import { recommend_page } from "@/api";
export default {
  name: "recommend",
  data() {
    return {
      visitorData: undefined,
      recommendList: [],
    };
  },
  methods: {
    onAdd() {
      // Toast("新增");
      this.$router.push("/recommend-add");
    },
    onEdit(item) {
      // Toast("编辑");
      const path = buildURLWithParams("/recommend-edit", item);
      // console.log('path', path)
      this.$router.push(path);
    },
    async fetchList() {
      this.visitorData = getLocal("visitorData");
      const res = await recommend_page({
        visitorId: this.visitorData.id,
        limit: 9999,
      });
      this.recommendList = [];
      res.data.list.map((item) => {
        if (item.def == "true") {
          this.recommendList.push(item);
        }
      });
      res.data.list.map((item) => {
        if (item.def != "true") {
          this.recommendList.push(item);
        }
      });
    },
  },
  created() {
    this.fetchList();
  },
  watch: {
    $route(to, from) {
      console.log("路由发生了变化：", to.path);
      if (to.path == "/recommend") {
        this.fetchList();
      }
      // console.log(this.contact);
    },
  },
};
</script>

<style lang="less" scoped>
.backGround {
  background: #f5f5f5;
  height: 100vh;
}
.recommend-container {
  padding: 0 10px;
  background: #f5f5f5;
  // height: 100vh;
  max-width: 400px;
  margin: 0 auto;
  // padding: 20px;
}
.title-padding {
  padding-top: 5vh;
}
.van-doc-demo-block__title {
  margin: 0;
  padding: 32px 16px 16px;
  color: rgba(69, 90, 100, 0.6);
  font-weight: normal;
  font-size: 14px;
  line-height: 16px;
}
.contact-card {
  margin-bottom: 2vh;
}
.bottomBox {
  padding-bottom: 7vh;
}
</style>
